<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/jquery.min.js"></script>
</head>
<body>
    <input type="file" name="" id="file">
    <button id="btnUpload">上传</button>
    <img src="./images/loading.gif" alt="" id="img" style="display: none;">
    <script>
        $(function(){
            // 坚听到ajax请求被发起了
            $(document).ajaxStart(function(){
                $('#img').show();
            })
            $(document).ajaxStop(function() {
                $('img').hide();
            })
            $('#btnUpload').on('click',function(){
                var files = $('#file')[0].files;
                if (files.length <= 0) {
                    return alert('请提交文件');
                }
                var fd = new FormData();
                fd.append('avatar',files[0]);
                $.ajax({
                    method:'post',
                    url:'http://www.liulongbin.top:3006/api/upload/avatar',
                    data:fd,
                    contentType:false,
                    processData:false,
                    success: function(res) {
                        console.log(res);
                    }
                })
            })
        })
    </script>
</body>
</html>